//
//  GQHBaseController.m
//  GQHKit
//
//  Created by GuanQinghao on 24/09/2017.
//  Copyright © 2017 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View

#pragma mark Controller
#import "GQHBaseController.h"

#import "GQHDriverProfileController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHBaseController () <UIGestureRecognizerDelegate>{
    
    CGFloat statusBarHeight;
    CGFloat navigationBarHeight;
    CGFloat tabBarHeight;
    CGFloat homeIndicatorHeight;
}

@end

@implementation GQHBaseController

#pragma mark --Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self.view setBackgroundColor:kWhiteColor];
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController.interactivePopGestureRecognizer setDelegate:self];
    
    [self loadNavigationBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    DLog();
    
}

#pragma mark --Service
/// 导航栏左键
- (IBAction)qh_clickedLeftButton:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

/// 导航栏右键
- (IBAction)qh_clickedRightButton:(UIButton *)sender {
    
}

/// 检查用户访问权限 AccessPermissions
- (BOOL)qh_checkUserAccessPermission {
    
    if ([USER_GET_STATUS isEqualToString:USER_STATUS_S0] || [USER_GET_STATUS isEqualToString:USER_STATUS_S4]) {
        /// 所有已认证的角色和填写基本信息的司机可以访问
        
        return YES;
    } else {
        
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            /// 司机填写基本信息和认证信息
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"当前用户尚未认证" preferredStyle:UIAlertControllerStyleAlert];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"填写基本信息" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                DLog(@"跳转填写司机基本信息页面");
                
                GQHDriverProfileController *drvierProfileController = [[GQHDriverProfileController alloc] init];
                drvierProfileController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:drvierProfileController animated:YES];
            }]];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"去认证" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                DLog(@"跳转司机认证信息页面");
                
                
            }]];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
            
            [self presentViewController:alertController animated:YES completion:nil];
        } else {
            
            /// 用户未认证
            [self qh_alertWithTitle:nil message:@"当前用户尚未认证" actionTitle:@"去认证" handler:^(UIAlertAction *action) {
                DLog(@"跳转货主、无车承运人和物流企业认证信息页面");
                
                if ([USER_GET_TYPE isEqualToString:USER_TYPE_S]) {
                    DLog(@"无车承运人");
                    
                }
                
                if ([USER_GET_TYPE isEqualToString:USER_TYPE_E]) {
                    DLog(@"物流企业");
                    
                }
                
                if ([USER_GET_TYPE isEqualToString:USER_TYPE_O]) {
                    DLog(@"货主");
                    
                }
                
                if ([USER_GET_TYPE isEqualToString:USER_TYPE_U]) {
                    DLog(@"未知角色");
                    
                }
            } completion:nil];
        }
        
        return NO;
    }
}

/// 添加、移除加载指示视图
- (void)qh_loadLoadingView {
    
    CGFloat width = 60;
    CGFloat height = 60;
    
    /**
    /// weak引用写法
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc]init];
    loadingView.qh_size = CGSizeMake(width, height);
    loadingView.center = self.view.center;
    loadingView.backgroundColor = kLightStrokeColor;
    loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    loadingView.color = kDarkBlackColor;
    loadingView.layer.cornerRadius = 10.0;
    [loadingView startAnimating];
    _qh_loadingView = loadingView;
    */
    
    /// strong引用写法
    _qh_loadingView = [[UIActivityIndicatorView alloc]init];
    _qh_loadingView.qh_size = CGSizeMake(width, height);
    _qh_loadingView.center = self.view.center;
    _qh_loadingView.backgroundColor = kLightStrokeColor;
    _qh_loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    _qh_loadingView.color = kDarkBlackColor;
    _qh_loadingView.layer.cornerRadius = 10.0;
    [_qh_loadingView startAnimating];
    
    [self.view addSubview:_qh_loadingView];
    [self.view bringSubviewToFront:_qh_loadingView];
}

- (void)qh_removedLoadingView {
    
    if (_qh_loadingView) {
        
        [_qh_loadingView removeFromSuperview];
    }
}

// 添加、移除加载错误视图
- (void)qh_loadErrorView {
    
    _qh_errorView = [[UIView alloc] init];
    _qh_errorView.frame = [UIScreen mainScreen].bounds;
    _qh_errorView.backgroundColor = kRedColor;
    [self.view addSubview:_qh_errorView];
    [self.view bringSubviewToFront:_qh_errorView];
}

- (void)qh_removedErrorView {
    
    if (_qh_errorView) {
        
        [_qh_errorView removeFromSuperview];
    }
}

#pragma mark --Setter/Getter
/// 修改状态栏样式
- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

#pragma mark --View
- (void)loadNavigationBar {
    
    _qh_navigationBar = [[UIView alloc]init];
    _qh_navigationBar.frame = CGRectMake(0, 0, self.view.qh_contentAreaWidth, self.view.qh_statusBarHeight + self.view.qh_navigationBarHeight);
    _qh_navigationBar.backgroundColor = kAppRedColor;
    [self.view addSubview:_qh_navigationBar];
    
    _qh_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _qh_leftButton.frame = CGRectMake(0, self.view.qh_statusBarHeight, self.view.qh_navigationBarHeight, self.view.qh_navigationBarHeight);
    _qh_leftButton.titleLabel.font = kDefaultFont;
    _qh_leftButton.titleLabel.textColor = kWhiteColor;
    _qh_leftButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_qh_leftButton setImage:[UIImage imageNamed:@"basic_navigationbar_leftarrow"] forState:UIControlStateNormal];
    [_qh_leftButton addTarget:self action:@selector(qh_clickedLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [_qh_navigationBar addSubview:_qh_leftButton];
    
    _qh_titleLabel = [[UILabel alloc]init];
    _qh_titleLabel.frame = CGRectMake(self.view.qh_navigationBarHeight, self.view.qh_statusBarHeight, self.view.qh_contentAreaWidth - (2 * self.view.qh_navigationBarHeight), self.view.qh_navigationBarHeight);
    _qh_titleLabel.font = kBiggerFont;
    _qh_titleLabel.textColor = kWhiteColor;
    _qh_titleLabel.textAlignment = NSTextAlignmentCenter;
    [_qh_navigationBar addSubview:_qh_titleLabel];
    
    _qh_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _qh_rightButton.frame = CGRectMake(self.view.qh_contentAreaWidth - self.view.qh_navigationBarHeight - 20, self.view.qh_statusBarHeight, self.view.qh_navigationBarHeight, self.view.qh_navigationBarHeight);
    _qh_rightButton.titleLabel.font = kDefaultFont;
    _qh_rightButton.titleLabel.textColor = kWhiteColor;
    _qh_rightButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_qh_rightButton setTitle:nil forState:UIControlStateNormal];
    [_qh_rightButton addTarget:self action:@selector(qh_clickedRightButton:) forControlEvents:UIControlEventTouchUpInside];
    [_qh_navigationBar addSubview:_qh_rightButton];
}

@end
